package ClassPackage;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.annotations.Entity;
import javax.persistence.Id;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Pipaillon
 */
@Entity
public class Personne implements Serializable{

    @Id
    private int id;

    private String name;
    private String lastName;


   public Personne()
    {
    }
    
    Personne(String firstname, String familyName)
    {
        this.name = firstname;
        this.lastName= familyName;
    }

    /**
     * @return the id
     */
    public int getID() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setID(int id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

     public List getListPersonne(SessionFactory cfg, String Name)
    {

        Session hibernate =cfg.getCurrentSession();
        hibernate.getTransaction().begin();
        Query query;

         if(Name.compareTo("none")!=0)
         {
    
             Name = "%" + Name + "%";
             String requete= "from Personne p where p.name like :aname ";
             query = hibernate.createQuery(requete);
             query.setString("aname",Name);
              
         }
         else
         {
            String requete= "from Personne";
            query = hibernate.createQuery(requete);
         }

         List<Personne> results = query.list();

         if(results.isEmpty())
         {
             // retourne une liste vide
             return new ArrayList();
         }
         else
         {
              return results;
         }

    }
}
